/*
 * $Id: SysCodeQuery.java 17 2014-08-28 10:52:50Z ratking $
 *
 * Copyleft (C) 2014 RatKing. All wrongs reserved.
 */
package cn.ratking.demo.jaws7.system.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;

import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.util.Messages;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;

import cn.ratking.demo.jaws7.model.LogEvent;
import cn.ratking.demo.jaws7.model.SysCode;
import cn.ratking.demo.jaws7.system.vo.SysCodeSearchCriteria;
import cn.ratking.demo.jaws7.system.service.SysCodeService;

/**
 * 系统代码查询
 */
@Named
@ViewScoped
public class SysCodeQuery implements Serializable {

    @Inject
    private transient Logger log;
    /** 事件源 */
    @Inject
    private Event<LogEvent> logEventSrc;
    @Inject
    private transient SysCodeService sysCodeService;
    @Inject
    private SysCodeSearchCriteria searchCriteria;
    private LazyDataModel<SysCode> resultModel;
    //private SysCode selectedSysCode;
    private SysCode[] selectedSysCodes;

    @PostConstruct
    public void init() {
        //log.debug("Debug: SysCodeQuery.init(), this=" + this);
    }

    @PreDestroy
    public void destroy() {
        //log.debug("Debug: SysCodeQuery.destroy(), this=" + this);
    }

//    public SysCode getSelectedSysCode() {
//        log.debug("Debug: SysCodeQuery.getSelectedSysCode(), selectedSysCode=" + selectedSysCode);
//        return selectedSysCode;
//    }
//
//    public void setSelectedSysCode(SysCode selectedSysCode) {
//        this.selectedSysCode = selectedSysCode;
//        log.debug("Debug: SysCodeQuery.setSelectedSysCode(), selectedSysCode=" + selectedSysCode);
//    }
    public SysCode[] getSelectedSysCodes() {
        //log.debug("Debug: SysCodeQuery.getSelectedSysCodes(), selectedSysCodes=" + selectedSysCodes);
        return selectedSysCodes;
    }

    public void setSelectedSysCodes(SysCode[] selectedSysCodes) {
        this.selectedSysCodes = selectedSysCodes;
        //log.debug("Debug: SysCodeQuery.setSelectedSysCodes(), selectedSysCodes=" + selectedSysCodes);
    }

    /**
     * 懒加载获得结果列表
     *
     * @return 结果列表
     */
    public LazyDataModel<SysCode> getResultModel() {
        if (resultModel == null) {
            //log.debug("Debug: SysCodeQuery.getResultModel(), resultModel = new LazyDataModel<SysCode>()");
            resultModel = new LazyDataModel<SysCode>() {
                @Override
                public List<SysCode> load(int first, int pageSize,
                        String sortField, SortOrder sortOrder,
                        Map<String, Object> filters) {
                    //log.debug("Debug: SysCodeQuery.getResultModel().load(), first=" + first + ", pageSize=" + pageSize);
//                    int rowCount = this.getRowCount();
//                    if (rowCount == 0) {
//                        return new ArrayList<SysCode>(0);
//                    }
//                    if (first >= rowCount) {
//                        // 当查询使用的首记录超出记录总数时，调整首记录
//                        if (rowCount % pageSize == 0) {
//                            first = ((rowCount / pageSize) - 1) * pageSize;
//                        } else {
//                            first = (rowCount / pageSize) * pageSize;
//                        }
//                    }
                    return sysCodeService.getResultList(first, pageSize,
                            sortField, sortOrder, searchCriteria);
                }

                @Override
                public SysCode getRowData(String rowKey) {
                    return (SysCode) sysCodeService.findById(Long.valueOf(rowKey));
                }

                @Override
                public Object getRowKey(SysCode object) {
                    return object.getSysCodeId().toString();
                }
            };

            //log.debug("Debug: SysCodeQuery.getResultModel(), resultModel.setRowCount(), rowCount=" + sysCodeService.getRowCount(searchCriteria));
            resultModel.setRowCount(sysCodeService.getRowCount(searchCriteria));
//            // Arbitrary value only for test purposes.
//            // @see http://forum.primefaces.org/viewtopic.php?f=3&t=4956
            //resultModel.setPageSize(20);
        }
        return resultModel;
    }

    /**
     * 根据ID删除系统代码
     *
     * @param sysCodeId 系统代码ID
     */
    public void deleteById(Long sysCodeId) {
        sysCodeService.deleteById(sysCodeId);
        String msg = "删除系统代码[" + sysCodeId + "]成功";
        log.debug(msg);
        logEventSrc.fire(new LogEvent(LogEvent.LOGTYPE_SYSTEM, LogEvent.LOGLEVEL_INFO, msg));
        Messages.addGlobalInfo(msg);
        refresh();
    }

    /**
     * 批量删除系统代码
     */
    public void batchDelete() {
        log.debug("开始调用 batchDelete()批量删除系统代码, selectedSysCodes="
                + selectedSysCodes);
        if (selectedSysCodes == null || selectedSysCodes.length == 0) {
            String msg = "批量删除时没有选择系统代码";
            log.warn(msg);
            Messages.addGlobalWarn(msg);
            return;
        }

        List<Long> sysCodeIdList = new ArrayList<Long>(selectedSysCodes.length);
        for (SysCode tmpSysCode : selectedSysCodes) {
            sysCodeIdList.add(tmpSysCode.getSysCodeId());
        }
        sysCodeService.batchDelete(sysCodeIdList);
        String msg = "批量删除系统代码成功";
        log.debug(msg);
        Messages.addGlobalInfo(msg);
        logEventSrc.fire(new LogEvent(LogEvent.LOGTYPE_SECURITY, LogEvent.LOGLEVEL_WARN, msg));

        refresh();
    }

    /**
     * 重置代码类型
     */
    public void resetCodeType() {
        sysCodeService.resetCodeType();
        String msg = "重置代码类型成功";
        log.debug(msg);
        logEventSrc.fire(new LogEvent(LogEvent.LOGTYPE_SYSTEM, LogEvent.LOGLEVEL_INFO, msg));
        Messages.addGlobalInfo(msg);
    }

    public void search() {
        refresh();
    }

    private void refresh() {
        resultModel = null;
    }
}
